package cn.com.css.taxInspection.common.sysjob.service;

import cn.com.css.taxInspection.common.constants.Constants;
import cn.com.css.taxInspection.common.sysjob.utils.LogUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;

@Service
public class GzcbBusinessService {

    private static final Logger logger = LoggerFactory.getLogger(GzcbBusinessService.class);
    private final JdbcTemplate jdbcTemplate; // 使用 final 关键字确保依赖是不可变的
    private final LogUtils logUtils;

    @Autowired
    public GzcbBusinessService(JdbcTemplate jdbcTemplate, LogUtils logUtils) {
        this.jdbcTemplate = jdbcTemplate;  // 通过构造器注入 JdbcTemplate
        this.logUtils = logUtils;  // 通过构造器注入 JdbcTemplate
    }

    //关注查补定时任务方法
    public void checkSystemStatus() {
        // 删除关注查补表
        dropGzcb();
        // 重新创建关注查补表
        createGzcb();
    }

    private void createGzcb() {
        // 定义需要执行的 SQL 文件路径
        List<String> sqlFiles = Arrays.asList(
                "templates/sql/sy_jc_cbsktj.sql",
                "templates/sql/sy_jc_cbsktj1.sql",
                "templates/sql/sy_jc_zcsks.sql",
                "templates/sql/sy_jc_zcsks1.sql"
        );
        // 遍历执行每个 SQL 文件
        for (String sqlFile : sqlFiles) {
            executeSqlFromFile(sqlFile);
        }
    }

    private void executeSqlFromFile(String sqlFile) {
        String tableName = sqlFile.substring(sqlFile.lastIndexOf("/") + 1, sqlFile.lastIndexOf(".sql"));
        try {
            Resource resource = new ClassPathResource(sqlFile);
            StringBuilder sqlBuilder = new StringBuilder();
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    sqlBuilder.append(line).append("\n");
                }
            }

            String sql = sqlBuilder.toString();
            String[] sqlStatements = sql.split(";");

            for (String statement : sqlStatements) {
                statement = statement.trim();
                if (!statement.isEmpty()) {
                    jdbcTemplate.execute(statement);
                }
            }
            logger.info("表 [{}] 创建成功", tableName);
            logUtils.saveDsrwLog("01",Constants.DSRW_GZCB, Constants.DSRW_ZXCG, null, tableName);
        } catch (Exception e) {
            String errorMsg = String.format("错误信息: %s", e.getMessage());
            logger.info(errorMsg);
            logUtils.saveDsrwLog("01",Constants.DSRW_GZCB, Constants.DSRW_ZXSB, errorMsg, tableName);
        }
    }

    //删除关注查补相关表
    private void dropGzcb() {
        jdbcTemplate.execute("DROP TABLE IF EXISTS kf_jicha_helper.sy_jc_cbsktj;"); // 使用 execute 执行 DROP TABLE
        jdbcTemplate.execute("DROP TABLE IF EXISTS kf_jicha_helper.sy_jc_cbsktj1;");
        jdbcTemplate.execute("DROP TABLE IF EXISTS kf_jicha_helper.sy_jc_zcsks;");
        jdbcTemplate.execute("DROP TABLE IF EXISTS kf_jicha_helper.sy_jc_zcsks1;");
    }

}
